<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>滑动窗口示例</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f7fc;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 300px;
            text-align: center;
        }

        h1 {
            color: #4A90E2;
            font-size: 24px;
            margin-bottom: 20px;
        }

        input {
            padding: 10px;
            width: 80%;
            margin: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
        }

        button {
            padding: 10px 20px;
            background-color: #4A90E2;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            margin-top: 10px;
        }

        button:hover {
            background-color: #357ABD;
        }

        .result {
            margin-top: 20px;
        }

        .result ul {
            list-style-type: none;
            padding: 0;
        }

        .result li {
            background-color: #f0f8ff;
            padding: 8px;
            margin: 4px 0;
            border-radius: 4px;
            font-size: 14px;
            color: #333;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>滑动窗口切片</h1>
        <label for="arrayInput">输入数组 (用逗号分隔):</label>
        <input type="text" id="arrayInput" placeholder="例如: 1, 2, 3, 4, 5">
        <label for="nInput">窗口大小 (n):</label>
        <input type="number" id="nInput" placeholder="窗口大小" min="1">
        <button onclick="generateSlices()">生成切片</button>

        <div class="result" id="result">
            <!-- 显示结果 -->
        </div>
    </div>

    <script>
        const aperture = (arr, n) =>
            n > arr.length ? [] : arr.slice(n - 1).map((v, i) => arr.slice(i, i + n));

        function generateSlices() {
            const arrayInput = document.getElementById('arrayInput').value;
            const nInput = parseInt(document.getElementById('nInput').value);

            // 将输入的字符串转换成数组
            const arr = arrayInput.split(',').map(item => item.trim()).filter(item => item !== "");

            // 获取滑动窗口的切片
            const result = aperture(arr, nInput);

            // 显示结果
            const resultDiv = document.getElementById('result');
            resultDiv.innerHTML = ''; // 清空之前的结果

            if (result.length === 0) {
                resultDiv.innerHTML = '<p style="color: red;">输入无效，请确保窗口大小不超过数组长度。</p>';
            } else {
                const ul = document.createElement('ul');
                result.forEach(slice => {
                    const li = document.createElement('li');
                    li.textContent = `切片: [${slice.join(', ')}]`;
                    ul.appendChild(li);
                });
                resultDiv.appendChild(ul);
            }
        }
    </script>
</body>

</html>